package com.shen;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Author:shen
 * CreateDate:2020/5/31.
 * Function:
 */
public class MyTest02 {
    @Test
    public void test01(){
        /**
         *
         * Gets a calendar using the default time zone and locale. 使用的是默认的时区和区域设置
         * The<code>Calendar</code> returned is based on the current time 返回的是一个Calendar对象，根据当前时间
         * in the default time zone with the default
         * {@link Locale.Category#FORMAT FORMAT} locale.//默认设置
         *
         * @return a Calendar.
         */

        Calendar calendar=Calendar.getInstance();//由于是protected无法直接创建对象，
        String[] week={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        calendar.setTime(new Date());
        System.out.println(calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DATE)+"日"+"周："+week[calendar.get(Calendar.DAY_OF_WEEK)-1]);
        //2020年5月31日周：星期日
    }

    @Test
    public void test02(){
        Calendar calendar=Calendar.getInstance();//由于是protected无法直接创建对象，
        calendar.set(2017,6,12);
        calendar.add(Calendar.DATE,5*365);
        System.out.println(calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DATE)+"日");
    }

    @Test
    public void test03(){
        Calendar calendar01=Calendar.getInstance();//由于是protected无法直接创建对象，
        calendar01.set(2012,12-1,21);

        Calendar calendar02=Calendar.getInstance();
        calendar02.setTime(new Date());

        long day=(calendar02.getTimeInMillis()-calendar01.getTimeInMillis())/(1000*60*60*24);
        System.out.println(day);

    }


    @Test
    public void test04(){
        Calendar calendar=Calendar.getInstance();//由于是protected无法直接创建对象，
        calendar.set(2020,10-1,1);
        String[] week={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        System.out.println(calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DATE)+"日"+week[calendar.get(Calendar.DAY_OF_WEEK)-1]);
        //2020年10月1日星期四
    }

    @Test
    public void test05(){
        Calendar calendar=Calendar.getInstance();//由于是protected无法直接创建对象，
        calendar.set(Calendar.YEAR,2020);
        calendar.set(Calendar.DATE,1);
        boolean flag=true;
       do{
           calendar.add(Calendar.DATE,1);
           if(calendar.get(Calendar.DAY_OF_WEEK)==1){
               System.out.println((calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DATE)+"日");
           }
           if(calendar.get(Calendar.YEAR)==2021){
               flag=false;
           }
        }while (flag);
    }

    @Test
    public void test06(){
        Calendar calendar=Calendar.getInstance();//由于是protected无法直接创建对象，
        Date date;
        calendar.set(Calendar.YEAR,2021);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd hh");
        calendar.set(Calendar.MONTH,0);
        calendar.set(Calendar.WEEK_OF_MONTH,0);
        calendar.set(Calendar.HOUR,8);
        boolean flag=true;
        do{
            calendar.add(Calendar.DATE,-1);
            if(calendar.get(Calendar.DAY_OF_WEEK)==7&&(calendar.get(Calendar.WEEK_OF_MONTH)==3||calendar.get(Calendar.WEEK_OF_MONTH)==4)){

                date=calendar.getTime();
                System.out.println(simpleDateFormat.format(date));
            }
            if(calendar.get(Calendar.YEAR)==2019){
                flag=false;
            }
        }while (flag);


    }

    @Test
    public void test07(){
        Calendar calendar=Calendar.getInstance();//由于是protected无法直接创建对象，
        Date date;
        calendar.set(Calendar.YEAR,2020);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd hh");
        calendar.set(Calendar.MONTH,0);
        calendar.set(Calendar.WEEK_OF_MONTH,0);
        calendar.set(Calendar.HOUR,8);
        boolean flag=true;
        do{
            calendar.add(Calendar.DATE,1);
            if(calendar.get(Calendar.DAY_OF_WEEK)==7&&calendar.get(Calendar.WEEK_OF_MONTH)==1){

                date=calendar.getTime();
                System.out.println(simpleDateFormat.format(date));
            }
            if(calendar.get(Calendar.YEAR)==2021){
                flag=false;
            }
        }while (flag);


    }


}
